import gtk, os, config
conf=config.config()

class OpenImage:
    def __init__(self):
        self.filename=self.browse()
        pass
    def browse(self):
        file_open = gtk.FileChooserDialog(title="Select Image"
                    , action=gtk.FILE_CHOOSER_ACTION_OPEN
                    , buttons=(gtk.STOCK_CANCEL
                                , gtk.RESPONSE_CANCEL
                                , gtk.STOCK_OPEN
                                , gtk.RESPONSE_OK))
        file_open.set_icon_from_file(conf.picture_path+"logo.png")
        file_open.set_current_folder(conf.lastfolder) 
        filter = gtk.FileFilter()
        filter.set_name("Images")
        filter.add_mime_type("image/png")
        filter.add_mime_type("image/jpeg")
        filter.add_mime_type("image/gif")
        filter.add_mime_type("image/svg+xml")
        filter.add_mime_type("image/x-ico")
        filter.add_pattern("*.png")
        filter.add_pattern("*.jpg")
        filter.add_pattern("*.jpeg")
        filter.add_pattern("*.gif")
        filter.add_pattern("*.svg")
        filter.add_pattern("*.ico")
        file_open.add_filter(filter)
        
        """Init the return value"""
        result = None
        if file_open.run() == gtk.RESPONSE_OK:
            result = file_open.get_filename()
        file_open.destroy()
        
        return result
        
    def get_file(self):
        if self.filename!=None and os.path.isfile(self.filename):
            conf.lastfolder=""
            i=1
            for path in self.filename.split("/"):
                if (i<len(self.filename.split("/"))): conf.lastfolder+=path+"/"
                i+=1
            return self.filename
        return None

    
class GetFolder:
    def __init__(self, title="Choose project folder"):
    	self.title=title
        self.filename=self.save()
    def save(self):
        file_open = gtk.FileChooserDialog(title=self.title
                    , action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER
                    , buttons=(gtk.STOCK_CANCEL
                                , gtk.RESPONSE_CANCEL
                                , gtk.STOCK_OPEN
                                , gtk.RESPONSE_OK))
        file_open.set_icon_from_file(conf.picture_path+"logo.png")
        file_open.set_current_folder(conf.lastfolder)           
        """Init the return value"""
        result = None
        if file_open.run() == gtk.RESPONSE_OK:
            result = file_open.get_filename()
        file_open.destroy()
        
        return result
        
    def get_folder(self):
        if self.filename!=None:
            conf.lastfolder=self.filename
            return self.filename
        return None
    
def getPixbufFromName(iconName, size=48):
    pixbuf = None
    try:
        pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(iconName, size, size)
        path = iconName
    except:
        pass
    if pixbuf == None:
        print "cannot find icon:" + iconName
        return None
    if pixbuf.get_width() != size or pixbuf.get_height() != size:
        width=pixbuf.get_width()
        height=pixbuf.get_height()
        if width==height:
            width=size
            height=size
        else:    
            if width > size:
                height=height*size/width
                width=size
                if height > size:
                    width=width*size/height
                    height=size
        pixbuf = pixbuf.scale_simple(width, height, gtk.gdk.INTERP_HYPER)
    return pixbuf

def scale_pixbuf(pixbuf, size):
    width=pixbuf.get_width()
    height=pixbuf.get_height()
    if width==height:
        width=size
        height=size
    else:    
        if width > size:
            height=height*size/width
            width=size
            if height > size:
                width=width*size/height
                height=size
    pixbuf = pixbuf.scale_simple(width, height, gtk.gdk.INTERP_HYPER)
    return pixbuf

def execute(command, wait=True):
    pid=os.fork()
    if pid==0:
        os.execvp(command[0], command)
    if wait: os.waitpid(pid, 0)
    else: return pid

def get_name(path):
    name=path.split("/")
    return name[len(name)-1]

def get_name_no_ext(path):
    name=get_name(path)
    name=name.split(".")
    i=0
    name_noext=""
    while i<len(name)-1:
        name_noext+=name[i]
        i+=1
    return name_noext